package com.bjut.dao.impl;

import java.util.List;

import com.bjut.dao.UserDao;
import com.bjut.domain.Employee;
import com.bjut.domain.User;

public class UserDaoImpl extends HRglxtHibernateDaoSupport implements UserDao{
	public void save(User user)
	{ 
		getHibernateTemplate().save(user);
		
	}
	public void save(Employee employee)
	{ 
		getHibernateTemplate().save(employee);
		
	}
	public User get(int id){
		return (User) getHibernateTemplate().get(User.class, id);
	}

	public void delete(User user) {
		getHibernateTemplate().delete(user);
	}

	public void delete(int id) {
		getHibernateTemplate().delete(get(id));
	}
    public User findById(int id){
       	return (User)getHibernateTemplate().get(User.class, id);
    }
	public void update(User user) {
		getHibernateTemplate().update(user);
	}
	
	public User findUserByUsername(String username) {
		List result=getHibernateTemplate().find("from User as u where u.username = ?" , 
				new String[]{username});
		if (result != null && result.size() > 0)
			return (User) result.get(0);
		else 
			return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> findAllByPage(int pageNo, int pageSize) {
		if (pageNo < 1)
		{
			return null;
		}
		int offset = (pageNo - 1) * pageSize;
		return findByPage("from User as u order by u.id", offset , pageSize);
	}

	public User findUserByUsernameAndPassword(String username,
			String password) {
		List result=getHibernateTemplate().find("from User as u where u.username = ? and u.password=?" , 
				new String[]{username, password});
		if (result != null && result.size() > 0)
			return (User) result.get(0);
		else 
			return null;
	}

	public long getCount() {
		List userList = getHibernateTemplate().find("select count(u.id) from User as u");
		if (userList == null)
		{
			return 0;
		}
		return (Long)userList.get(0);
	}

	
}
